package com.eascs.web.p2p.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.p2p.common.entity.MsgTemplateEntity;
import com.eascs.p2p.common.service.IMsgTemplateService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.vo.MsgTemplateQueryVO;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.p2p.common.BaseMasterViewController;

@Controller
@RequestMapping("/template")
public class TemplateController extends BaseMasterViewController {

	@Reference
	IMsgTemplateService msgTemplateService;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "admin");
	}
	/**
	 * @Title: index
	 * @Description:模板列表
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws @date
	 *             2016年7月28日 上午10:39:39
	 */
	@RequestMapping(value = "/index")
	public String index(MsgTemplateQueryVO vo) {

		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model = msgTemplateService.page(vo);
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		if (model.isSuccess()) {
			pageResult = model.getModel();
		}

		String[] titles = { "模板编号", "名称", "模板类型", "状态" };

		AjaxButton openButton = new AjaxButton("启用                   ", "/templateRest/isDisplay?isDisplay=0&id=");
		openButton.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				return "停用".equals(params[4]);
			}

		});

		AjaxButton closeButton = new AjaxButton("停用                  ", "/templateRest/isDisplay?isDisplay=1&id=");
		closeButton.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				return "启用".equals(params[4]);
			}

		});

		RedirectButton Button3 = new RedirectButton("修改                   ", "/template/edit_template?id=");
		Button[] detailButton = new Button[] { openButton, closeButton, Button3 };

		String pageUrl = "/template/index";
		pageUrl = UrlUtil.appendUrl(pageUrl, "templateCode", vo.getTemplateCode());
		pageUrl = UrlUtil.appendUrl(pageUrl, "templateName", vo.getTemplateName());
		pageUrl = UrlUtil.appendUrl(pageUrl, "templateType", vo.getTemplateType());
		pageUrl = UrlUtil.appendUrl(pageUrl, "isLock", vo.getIsLock());
		pageUrl = UrlUtil.appendUrl(pageUrl, "showPage", "");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setBuildPagingNavigation(true);
		// paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);

		BaseRestResult.refresh();
		return "/template/index";
	}

	@RequestMapping(value = "/add_template")
	public String addBanner() {
		ViewContext.put("title", "添加");
		return "/template/template_edit";
	}

	@RequestMapping(value = "/edit_template")
	public String editBanner(String id) {
		ModelResult<MsgTemplateEntity> model = msgTemplateService.queryById(id);
		MsgTemplateEntity vo = new MsgTemplateEntity();
		if (model.isSuccess()) {
			vo = model.getModel();
		}
		ViewContext.put("vo", vo);
		ViewContext.put("title", "修改");
		return "/template/template_edit";
	}

}
